package cn.nit.blog.controller;

import cn.nit.blog.entity.Comment;
import cn.nit.blog.entity.User;
import cn.nit.blog.exception.BlogException;
import cn.nit.blog.service.ICommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wjx
 * @since 2023-03-25
 */
@Controller
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private ICommentService commentService;

    @PostMapping("/create")
    public String create(HttpSession session, Comment comment){

        User user = (User) session.getAttribute("loginUser");

//        判断用户登录
        if (user == null) {
            throw new BlogException(1004, "请先登录！");
        }

//        获取信息：帖子内容，回复的帖子等。。
        comment.setCreateTime(new Date());
        comment.setStatus(1);
        comment.setUserId(user.getId());


//        存入数据库
        commentService.save(comment);

//        重定向到当前页面
        return "redirect:/post/detail/"+comment.getEntityId();
    }

}
